package com.service.impl;

import com.alibaba.fastjson.JSON;
import com.mongodb.client.result.UpdateResult;
import com.pojo.*;
import com.pojoVO.CommentInfoVo;
import com.pojoVO.GroupComStarVO;
import com.pojoVO.GroupComVO;
import com.service.GroupCommentImgService;
import com.service.GroupCommentService;
import com.service.GroupOrderService;
import com.service.UserService;
import com.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.BasicUpdate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2020/3/19 0019.
 */
@Service
public class GroupCommentServiceImpl implements GroupCommentService {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    GroupOrderService groupOrderService;
    @Autowired
    GroupCommentImgService groupCommentImgService;
    @Autowired
    UserService userService;
    @Override
    public ServerResponse save(GroupComment groupComment) {
        SimpleDateFormat format=new SimpleDateFormat("MMmmss");
        String groupIdStr = format.format(new Date());
        Double floor = Math.floor(Math.random() * (9999 - 1000) + 1000);
        groupIdStr+=floor.toString();
        String[] split = floor.toString().split(".");
        Byte status =1;
        groupComment.setCommentId(1111);
        groupComment.setStatus(status);
        //执行mybatis添加方法
        GroupComment save = mongoTemplate.save(groupComment);
        //判断添加成功
        if (save!=null)
            return ServerResponse.addSuccess();
        else
            return ServerResponse.addFailed();
    }

    @Override
    public ServerResponse modifyStatus(Integer id) {
        Query query = new Query(Criteria.where("_id").is(id));
        Update update = new Update().set("status",Byte.valueOf("0"));
        UpdateResult updateResult = mongoTemplate.updateFirst(query, update, GroupComment.class);
        if (updateResult!=null)
            return ServerResponse.deleteSuccess();
        return ServerResponse.deleteFailed();
    }

    @Override
    public ServerResponse modify(GroupComment groupComment) {
        Query query = new Query(Criteria.where("_id").is(groupComment.getCommentId()));
        BasicUpdate update = new BasicUpdate(JSON.toJSONString(groupComment));
        UpdateResult updateResult= mongoTemplate.updateFirst(query,update,GroupComment.class);
        if (updateResult!=null)
            return ServerResponse.updateSuccess();
        return ServerResponse.updateFailed();
    }

    @Override
    public ServerResponse getById(Integer id) {
        Query query = new Query(Criteria.where("_id").is(id));
        GroupComment groupComment = mongoTemplate.findOne(query,GroupComment.class);
        if (groupComment!=null)
            return ServerResponse.getDataSuccess(groupComment);
        return ServerResponse.getDataFailed("没有该评论");
    }

    @Override
    public ServerResponse getAll() {
        Query query = new Query(Criteria.where("status").is(Byte.valueOf("1")));
        List<GroupComment> groupCommentList = mongoTemplate.find(query, GroupComment.class);
        if (groupCommentList!=null&&groupCommentList.size()>0)
            return ServerResponse.getDataSuccess(groupCommentList);
        return ServerResponse.getDataFailed("没有数据");
    }

    @Override
    public ServerResponse getByGroupId(String groupId) {
        Query query = new Query(Criteria.where("status").is(Byte.valueOf("1"))
                                        .and("groupId").is(Integer.valueOf(groupId))
                    );

        List<GroupComment> groupCommentList= mongoTemplate.find(query,GroupComment.class);
        List<GroupComVO> GroupComVOS =new ArrayList<>();
        if (groupCommentList!=null&&groupCommentList.size()>0) {
            for (GroupComment groupComment:groupCommentList){
                ServerResponse serverResponseImg = groupCommentImgService.getByCommentID(String.valueOf(groupComment.getCommentId()));
                GroupComVO GroupComVO =new GroupComVO();
                GroupComVO.setGroupComment(groupComment);
                if (serverResponseImg.getStatus()==1){
                    GroupComVO.setGroupCommentImgs((List<GroupCommentImg>)serverResponseImg.getData());
                }else {
                }
                GroupComVOS.add(GroupComVO);
            }
            return ServerResponse.getDataSuccess(GroupComVOS);
        }
        return ServerResponse.getDataFailed("没有数据");
    }

    @Override
    public ServerResponse getstarNumByGroupId(String groupId) {
        Query query = new Query(Criteria.where("status").is(Byte.valueOf("1"))
                .and("groupId").is(Integer.valueOf(groupId))
        );

        List<GroupComment> groupCommentList= mongoTemplate.find(query,GroupComment.class);
         Integer oneStarNum=0;
        Integer twoStarNum=0;
        Integer threeStarNum=0;
        Integer fourStarNum=0;
        Integer fiveStarNum=0;
        Integer allStarNum=0;
        Integer StarTol=0;
         Float avaStar=0f;


        if (groupCommentList!=null&&groupCommentList.size()>0) {
            for (GroupComment g : groupCommentList) {
                Integer star = Integer.valueOf(g.getStar());
                if (star != null) {
                    allStarNum++;
                    StarTol += star;
                    switch (star) {
                        case 1:
                            oneStarNum++;
                            break;
                        case 2:
                            twoStarNum++;
                            break;
                        case 3:
                            threeStarNum++;
                            break;
                        case 4:
                            fourStarNum++;
                            break;
                        case 5:
                            fiveStarNum++;
                            break;
                    }
                }
            }
            DecimalFormat df=new DecimalFormat("0.0");
            avaStar=(float)StarTol/allStarNum;
            String format = df.format(avaStar);
           // String format=avaStar.toString().substring(4);
            System.out.println("avaStar=="+avaStar);
            System.out.println("format=="+format);
            GroupComStarVO groupComStarVO = new GroupComStarVO(oneStarNum,twoStarNum,threeStarNum,
                                                                fourStarNum,fiveStarNum,allStarNum,Float.valueOf(format));
            return ServerResponse.getDataSuccess(groupComStarVO);
        }
        return ServerResponse.getDataFailed("没有数据");
    }

    @Override
    public ServerResponse getByOrderNum(Long orderNum) {
        Query query = new Query(Criteria.where("status").is(Byte.valueOf("1"))
                .and("other2").is(orderNum)
        );

        List<GroupComment> groupCommentList= mongoTemplate.find(query,GroupComment.class);

        if (groupCommentList!=null&&groupCommentList.size()>0){
            return ServerResponse.getDataSuccess(groupCommentList.get(0));
        }
        return ServerResponse.getDataFailed("没有数据");
    }

    @Override
    public ServerResponse submitCommet(CommentInfoVo commentInfoVo) {
        Long orderNum = commentInfoVo.getOrderNum();
        ServerResponse groupOrderst = groupOrderService.getByOrderNum(orderNum);
        if (groupOrderst.getStatus()!=1)
            return groupOrderst;
        GroupOrder groupOrder =(GroupOrder)groupOrderst.getData();
        Integer userId = groupOrder.getUserId();
        Integer groupId = groupOrder.getGroupId();
        ServerResponse usersr = userService.getById(userId);
        User user =(User) usersr.getData();
        String userName = user.getUserName();
        GroupComment groupComment=new GroupComment();
        groupComment.setUserId(userId);
        groupComment.setGroupId(groupId);
        groupComment.setStar(commentInfoVo.getStar().toString());
        groupComment.setCommentContext(commentInfoVo.getCommentContext());
        groupComment.setCreatetime(new Date());
        groupComment.setOther1(userName);
        groupComment.setOther2(orderNum.toString());
        groupComment.setStatus(Byte.valueOf("1"));
        this.save(groupComment);
        ServerResponse newCommentsr = this.getByOrderNum(orderNum);
        if (newCommentsr.getStatus()!=1)
            return newCommentsr;
        GroupComment newComment =(GroupComment)newCommentsr.getData();
        System.out.println("查询出信息"+newComment.toString());
        Integer newCommentId = newComment.getCommentId();
        String[] imgList = commentInfoVo.getImgList();
        if (imgList!=null&&imgList.length>0){
            for (String img:imgList){
                GroupCommentImg groupCommentImg=new GroupCommentImg();
                groupCommentImg.setSmall(img);
                groupCommentImg.setCommId(newCommentId);
                groupCommentImg.setCreatetime(new Date());
                groupCommentImg.setStatus(Byte.valueOf("1"));
                groupCommentImgService.save(groupCommentImg);
            }
        }
        return ServerResponse.addSuccess();
    }
}
